<template>
  <div class="prommise-box">
    <el-card>
      <div style="text-align: right">
        <el-button
          icon="el-icon-edit"
          type="primary"
          size="mini"
          @click="addClick('0', 1)"
        >添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addClick(row.id, 1)"
            >添加页面权限点</el-button>
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addClick(row.id, 2)"
            >添加按钮权限点</el-button>
            <el-button type="text" @click="editClick(row)">修改</el-button>
            <el-button type="text" @click="delClick(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增组件 -->
    <Add ref="add" @getData="getData" />
  </div>
</template>

<script>
import { sysPermission, sysPermissionDel } from '@/api/permisse'
import Add from './component/Add.vue'
export default {
  components: {
    Add
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getData()
  },
  // 获取权限菜单数据
  methods: {
    async getData() {
      const res = await sysPermission()
      console.log(res)
      this.list = this.changeData(res.data, '0')
      console.log(this.list)
    },
    // 处理数据将数据处理成树形结构
    changeData(arr, str) {
      // 获得一层数据
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 打开弹框组件
    addClick(pid, type) {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
      this.$refs.add.form.pid = pid
      this.$refs.add.form.type = type
    },
    // 编辑按钮
    editClick(row) {
      // 打开弹框
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'edit'
      // 数据回显row是改行的数据
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 删除
    delClick(id) {
      // 确定删除框
      this.$confirm('您确定删除吗？', '温馨提示')
        .then(async() => {
          await sysPermissionDel(id)
          // 提示删除成功
          this.$message.success('删除成功')
          // 刷新父级列表
          this.getData()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.prommise-box {
  padding: 15px;
}
</style>
